package com.dependencies.springjdbc.controller;

import com.dependencies.springjdbc.model.Student;
import com.dependencies.springjdbc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zhizhou   2024/8/23 09:21
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    
    @Autowired
    private StudentService StudentService;
    
    @PostMapping("/add")
    public String addStudent(@RequestBody Student Student) {
        int result = StudentService.addStudent(Student);
        return result > 0 ? "Student added successfully!" : "Failed to add Student!";
    }
    
    @GetMapping("/all")
    public List<Student> findAllStudents() {
        return StudentService.findAllStudents();
    }
    
    @GetMapping("/{id}")
    public Student findStudentById(@PathVariable Long id) {
        return StudentService.findStudentById(id);
    }
    
    @PutMapping("/update")
    public String updateStudent(@RequestBody Student Student) {
        int result = StudentService.updateStudent(Student);
        return result > 0 ? "Student updated successfully!" : "Failed to update Student!";
    }
    
    @DeleteMapping("/delete/{id}")
    public String deleteStudent(@PathVariable Long id) {
        int result = StudentService.deleteStudent(id);
        return result > 0 ? "Student deleted successfully!" : "Failed to delete Student!";
    }
    
    @GetMapping("/map/{id}")
    public Map<String, Object> findStudentAsMap(@PathVariable Long id) {
        return StudentService.findStudentAsMap(id);
    }
    
    @GetMapping("/maplist")
    public List<Map<String, Object>> findAllStudentsAsMapList() {
        return StudentService.findAllStudentsAsMapList();
    }
    
}
